package com.samoiloff.ui.scroll.resize.view {
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * @author Samoiloff
	 */
	public class ArrowView extends EventDispatcher {
		
		private var _mc: Sprite;		
		
		private var _normal: Bitmap;
		private var _over: Bitmap;
		
		public function ArrowView(normBmdt:Bitmap, overBmdt:Bitmap) {
			_normal = normBmdt;
			_over = overBmdt;
			
			_mc = new Sprite();
			_mc.addChild(_normal);
			_mc.addChild(_over);
			
			_mc.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			_mc.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_mc.addEventListener(MouseEvent.CLICK, onClick);
			
			_mc.useHandCursor = _mc.buttonMode = true;
			onMouseOut();
		}
		
		public function destroy():void {
			_mc.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			_mc.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_mc.removeEventListener(MouseEvent.CLICK, onClick);
			_mc = null;
		}

		private function onMouseOver(e:MouseEvent): void {
			_normal.visible = false;
			_over.visible = true;
		}

		private function onMouseOut(e:MouseEvent = null): void {
			_normal.visible = true;
			_over.visible = false;
		}

		private function onClick(e:MouseEvent): void {
			dispatchEvent(e);
		}
		
		public function get mc(): Sprite {
			return _mc;
		}
		
		public function set enabled(enabled: Boolean): void {
			_mc.mouseEnabled = _mc.mouseChildren = enabled;
			_mc.alpha = enabled ? 1 : .5;
		}
	}
}
